﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace Smith.ImageConverter
{
    public abstract class ImageConverter
    {
        public abstract void Save(Bitmap image, string path);

        public static ImageCodecInfo GetImageCodeInfo(string type)
        {
            ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders();
            foreach (ImageCodecInfo i in info)
                if (i.MimeType == type) return i;
            return null;
        }

        public ImageCodecInfo CodeInfo { get; protected set; }
        public EncoderParameters Parameters { get; protected set; }
    }

    public class JPGConverter : ImageConverter
    {
        public JPGConverter(long quality)
        {
            Quality = quality;
            CodeInfo = GetImageCodeInfo("image/jpeg");
            Parameters = new EncoderParameters();
            Parameters.Param = new EncoderParameter[] { 
                new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality) };
        }

        public override void Save(Bitmap image, string path)
        {
            if (image.Tag.Equals(ImageFormat.Png))
            {
                using (Bitmap img = new Bitmap(image.Width, image.Height))
                {
                    using (Graphics g = Graphics.FromImage(img))
                    {
                        g.FillRectangle(Brushes.White, 0, 0, img.Width, img.Height);
                        g.DrawImage(image, 0, 0, image.Width, image.Height);
                        img.Save(path, CodeInfo, Parameters);
                    }
                }
            }
            else
            {
                image.Save(path, CodeInfo, Parameters);
            }
        }

        public long Quality { get; private set; }
    }

    public class PNGConverter : ImageConverter
    {
        public PNGConverter()
        {
            CodeInfo = GetImageCodeInfo("image/png");
            Parameters = new EncoderParameters();
        }

        public override void Save(Bitmap image, string path)
        {
            image.Save(path, ImageFormat.Png);
        }
    }

    public class BMPConverter : ImageConverter
    {
        public BMPConverter()
        {
            CodeInfo = GetImageCodeInfo("image/bmp");
            Parameters = new EncoderParameters();
        }

        public override void Save(Bitmap image, string path)
        {
            if (image.Tag.Equals(ImageFormat.Png))
            {
                using (Bitmap img = new Bitmap(image.Width, image.Height))
                {
                    using (Graphics g = Graphics.FromImage(img))
                    {
                        g.FillRectangle(Brushes.White, 0, 0, img.Width, img.Height);
                        g.DrawImage(image, 0, 0, image.Width, image.Height);
                        img.Save(path, ImageFormat.Bmp);
                    }
                }
            }
            else
            {
                image.Save(path, ImageFormat.Bmp);
            }
        }
    }
}
